<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>In.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="In code in Java">
<meta NAME="TITLE" CONTENT="In code in Java">
<meta NAME="KEYWORDS" CONTENT="In,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>In.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "In.java">In.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac In.java</span>
<span class="comment"> *  Execution:    java In   (basic test --- see source for required files)</span>
<span class="comment"> *  Dependencies: none</span>
<span class="comment"> *</span>
<span class="comment"> *  Reads in data of various types from standard input, files, and URLs.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">BufferedInputStream</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">File</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">IOException</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">InputStream</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">net</span><span class="symbol">.</span><span class="normal">URL</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">net</span><span class="symbol">.</span><span class="normal">Socket</span><span class="symbol">;</span>
<span class="comment">// import java.net.HttpURLConnection;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">net</span><span class="symbol">.</span><span class="normal">URLConnection</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">ArrayList</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">InputMismatchException</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Locale</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">NoSuchElementException</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Scanner</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">regex</span><span class="symbol">.</span><span class="normal">Pattern</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Input</span><span class="keyword">&lt;/i&gt;</span><span class="comment">. This class provides methods for reading strings</span>
<span class="comment"> *  and numbers from standard input, file input, URLs, and sockets. </span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The Locale used is: language = English, country = US. This is consistent</span>
<span class="comment"> *  with the formatting conventions with Java floating-point literals,</span>
<span class="comment"> *  command-line arguments (via {</span><span class="type">@link</span><span class="comment"> Double#parseDouble(String)})</span>
<span class="comment"> *  and standard output. </span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span>
<span class="comment"> *  </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://introcs.cs.princeton.edu/31datatype"</span><span class="keyword">&gt;</span><span class="comment">Section 3.1</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Introduction to Programming in Java: An Interdisciplinary Approach</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> </span>
<span class="comment"> *  by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Like {</span><span class="type">@link</span><span class="comment"> Scanner}, reading a token also consumes preceding Java</span>
<span class="comment"> *  whitespace, reading a full line consumes</span>
<span class="comment"> *  the following end-of-line delimeter, while reading a character consumes</span>
<span class="comment"> *  nothing extra. </span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Whitespace is defined in {</span><span class="type">@link</span><span class="comment"> Character#isWhitespace(char)}. Newlines</span>
<span class="comment"> *  consist of \n, \r, \r\n, and Unicode hex code points 0x2028, 0x2029, 0x0085;</span>
<span class="comment"> *  see </span><span class="keyword">&lt;tt&gt;&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://www.docjar.com/html/api/java/util/Scanner.java.html"</span><span class="keyword">&gt;</span>
<span class="comment"> *  Scanner.java</span><span class="keyword">&lt;/a&gt;&lt;/tt&gt;</span><span class="comment"> (NB: Java 6u23 and earlier uses only \r, \r, \r\n).</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> David Pritchard</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">In</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">///// begin: section (1 of 2) of code duplicated from In to StdIn.</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">// assume Unicode UTF-8 encoding</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> CHARSET_NAME </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"UTF-8"</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// assume language = English, country = US for consistency with System.out.</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Locale</span><span class="normal"> LOCALE </span><span class="symbol">=</span><span class="normal"> Locale</span><span class="symbol">.</span><span class="normal">US</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// the default token separator; we maintain the invariant that this value </span>
<span class="normal">    </span><span class="comment">// is held by the scanner's delimiter between calls</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Pattern</span><span class="normal"> WHITESPACE_PATTERN</span>
<span class="normal">        </span><span class="symbol">=</span><span class="normal"> Pattern</span><span class="symbol">.</span><span class="function">compile</span><span class="symbol">(</span><span class="string">"</span><span class="specialchar">\\</span><span class="string">p{javaWhitespace}+"</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// makes whitespace characters significant </span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Pattern</span><span class="normal"> EMPTY_PATTERN</span>
<span class="normal">        </span><span class="symbol">=</span><span class="normal"> Pattern</span><span class="symbol">.</span><span class="function">compile</span><span class="symbol">(</span><span class="string">""</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// used to read the entire input. source:</span>
<span class="normal">    </span><span class="comment">// http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Pattern</span><span class="normal"> EVERYTHING_PATTERN</span>
<span class="normal">        </span><span class="symbol">=</span><span class="normal"> Pattern</span><span class="symbol">.</span><span class="function">compile</span><span class="symbol">(</span><span class="string">"</span><span class="specialchar">\\</span><span class="string">A"</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">//// end: section (1 of 2) of code duplicated from In to StdIn.</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Scanner</span><span class="normal"> scanner</span><span class="symbol">;</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes an input stream from standard input.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">In</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        scanner </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Scanner</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedInputStream</span><span class="symbol">(</span><span class="normal">System</span><span class="symbol">.</span><span class="normal">in</span><span class="symbol">),</span><span class="normal"> CHARSET_NAME</span><span class="symbol">);</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useLocale</span><span class="symbol">(</span><span class="normal">LOCALE</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes an input stream from a socket.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  socket the socket</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if cannot open {</span><span class="type">@code</span><span class="comment"> socket}</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if {</span><span class="type">@code</span><span class="comment"> socket} is {</span><span class="type">@code</span><span class="comment"> null}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="usertype">Socket</span><span class="normal"> socket</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">socket </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">InputStream</span><span class="normal"> is </span><span class="symbol">=</span><span class="normal"> socket</span><span class="symbol">.</span><span class="function">getInputStream</span><span class="symbol">();</span>
<span class="normal">            scanner </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Scanner</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedInputStream</span><span class="symbol">(</span><span class="normal">is</span><span class="symbol">),</span><span class="normal"> CHARSET_NAME</span><span class="symbol">);</span>
<span class="normal">            scanner</span><span class="symbol">.</span><span class="function">useLocale</span><span class="symbol">(</span><span class="normal">LOCALE</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> ioe</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Could not open "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> socket</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes an input stream from a URL.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  url the URL</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if cannot open {</span><span class="type">@code</span><span class="comment"> url}</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if {</span><span class="type">@code</span><span class="comment"> url} is {</span><span class="type">@code</span><span class="comment"> null}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="usertype">URL</span><span class="normal"> url</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">url </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">URLConnection</span><span class="normal"> site </span><span class="symbol">=</span><span class="normal"> url</span><span class="symbol">.</span><span class="function">openConnection</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">InputStream</span><span class="normal"> is     </span><span class="symbol">=</span><span class="normal"> site</span><span class="symbol">.</span><span class="function">getInputStream</span><span class="symbol">();</span>
<span class="normal">            scanner            </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Scanner</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedInputStream</span><span class="symbol">(</span><span class="normal">is</span><span class="symbol">),</span><span class="normal"> CHARSET_NAME</span><span class="symbol">);</span>
<span class="normal">            scanner</span><span class="symbol">.</span><span class="function">useLocale</span><span class="symbol">(</span><span class="normal">LOCALE</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> ioe</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Could not open "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> url</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes an input stream from a file.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  file the file</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if cannot open {</span><span class="type">@code</span><span class="comment"> file}</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if {</span><span class="type">@code</span><span class="comment"> file} is {</span><span class="type">@code</span><span class="comment"> null}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="usertype">File</span><span class="normal"> file</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">file </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            scanner </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Scanner</span><span class="symbol">(</span><span class="normal">file</span><span class="symbol">,</span><span class="normal"> CHARSET_NAME</span><span class="symbol">);</span>
<span class="normal">            scanner</span><span class="symbol">.</span><span class="function">useLocale</span><span class="symbol">(</span><span class="normal">LOCALE</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> ioe</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Could not open "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes an input stream from a filename or web page name.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  name the filename or web page name</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if cannot open {</span><span class="type">@code</span><span class="comment"> name} as</span>
<span class="comment">     *         a file or URL</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if {</span><span class="type">@code</span><span class="comment"> name} is {</span><span class="type">@code</span><span class="comment"> null}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> name</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">name </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">// first try to read file from local file system</span>
<span class="normal">            </span><span class="usertype">File</span><span class="normal"> file </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">File</span><span class="symbol">(</span><span class="normal">name</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">file</span><span class="symbol">.</span><span class="function">exists</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                scanner </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Scanner</span><span class="symbol">(</span><span class="normal">file</span><span class="symbol">,</span><span class="normal"> CHARSET_NAME</span><span class="symbol">);</span>
<span class="normal">                scanner</span><span class="symbol">.</span><span class="function">useLocale</span><span class="symbol">(</span><span class="normal">LOCALE</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="comment">// next try for files included in jar</span>
<span class="normal">            </span><span class="usertype">URL</span><span class="normal"> url </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getClass</span><span class="symbol">().</span><span class="function">getResource</span><span class="symbol">(</span><span class="normal">name</span><span class="symbol">);</span>

<span class="normal">            </span><span class="comment">// or URL from web</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">url </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                url </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">URL</span><span class="symbol">(</span><span class="normal">name</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="usertype">URLConnection</span><span class="normal"> site </span><span class="symbol">=</span><span class="normal"> url</span><span class="symbol">.</span><span class="function">openConnection</span><span class="symbol">();</span>

<span class="normal">            </span><span class="comment">// in order to set User-Agent, replace above line with these two</span>
<span class="normal">            </span><span class="comment">// HttpURLConnection site = (HttpURLConnection) url.openConnection();</span>
<span class="normal">            </span><span class="comment">// site.addRequestProperty("User-Agent", "Mozilla/4.76");</span>

<span class="normal">            </span><span class="usertype">InputStream</span><span class="normal"> is     </span><span class="symbol">=</span><span class="normal"> site</span><span class="symbol">.</span><span class="function">getInputStream</span><span class="symbol">();</span>
<span class="normal">            scanner            </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Scanner</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedInputStream</span><span class="symbol">(</span><span class="normal">is</span><span class="symbol">),</span><span class="normal"> CHARSET_NAME</span><span class="symbol">);</span>
<span class="normal">            scanner</span><span class="symbol">.</span><span class="function">useLocale</span><span class="symbol">(</span><span class="normal">LOCALE</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> ioe</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Could not open "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> name</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an input stream from a given {</span><span class="type">@link</span><span class="comment"> Scanner} source; use with </span>
<span class="comment">     * </span><span class="keyword">&lt;tt&gt;</span><span class="comment">new Scanner(String)</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to read from a string.</span>
<span class="comment">     * </span><span class="keyword">&lt;p&gt;</span>
<span class="comment">     * Note that this does not create a defensive copy, so the</span>
<span class="comment">     * scanner will be mutated as you read on. </span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  scanner the scanner</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if {</span><span class="type">@code</span><span class="comment"> scanner} is {</span><span class="type">@code</span><span class="comment"> null}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="usertype">Scanner</span><span class="normal"> scanner</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">scanner </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">scanner </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if this input stream exists.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this input stream exists; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">exists</span><span class="symbol">()</span><span class="normal">  </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">////  begin: section (2 of 2) of code duplicated from In to StdIn,</span>
<span class="normal">    </span><span class="comment">////  with all methods changed from "public" to "public static".</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns true if input stream is empty (except possibly whitespace).</span>
<span class="comment">     * Use this to know whether the next call to {</span><span class="type">@link</span><span class="comment"> #readString()}, </span>
<span class="comment">     * {</span><span class="type">@link</span><span class="comment"> #readDouble()}, etc will succeed.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this input stream is empty (except possibly whitespace);</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isEmpty</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">scanner</span><span class="symbol">.</span><span class="function">hasNext</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/** </span>
<span class="comment">     * Returns true if this input stream has a next line.</span>
<span class="comment">     * Use this method to know whether the</span>
<span class="comment">     * next call to {</span><span class="type">@link</span><span class="comment"> #readLine()} will succeed.</span>
<span class="comment">     * This method is functionally equivalent to {</span><span class="type">@link</span><span class="comment"> #hasNextChar()}.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this input stream is empty;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasNextLine</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">hasNextLine</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if this input stream has more inputy (including whitespace).</span>
<span class="comment">     * Use this method to know whether the next call to {</span><span class="type">@link</span><span class="comment"> #readChar()} will succeed.</span>
<span class="comment">     * This method is functionally equivalent to {</span><span class="type">@link</span><span class="comment"> #hasNextLine()}.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this input stream has more input (including whitespace);</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise   </span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasNextChar</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">EMPTY_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">boolean</span><span class="normal"> result </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">hasNext</span><span class="symbol">();</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">WHITESPACE_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> result</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads and returns the next line in this input stream.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next line in this input stream; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such line</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">readLine</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> line</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            line </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextLine</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">NoSuchElementException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            line </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> line</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads and returns the next character in this input stream.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next character in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">char</span><span class="normal"> </span><span class="function">readChar</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">EMPTY_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> ch </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">next</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> ch</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">:</span><span class="normal"> </span><span class="string">"Internal (Std)In.readChar() error!"</span>
<span class="normal">            </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" Please contact the authors."</span><span class="symbol">;</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">WHITESPACE_PATTERN</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> ch</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal">  </span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads and returns the remainder of this input stream, as a string.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the remainder of this input stream, as a string</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">readAll</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">scanner</span><span class="symbol">.</span><span class="function">hasNextLine</span><span class="symbol">())</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="string">""</span><span class="symbol">;</span>

<span class="normal">        </span><span class="usertype">String</span><span class="normal"> result </span><span class="symbol">=</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">EVERYTHING_PATTERN</span><span class="symbol">).</span><span class="function">next</span><span class="symbol">();</span>
<span class="normal">        </span><span class="comment">// not that important to reset delimeter, since now scanner is empty</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">useDelimiter</span><span class="symbol">(</span><span class="normal">WHITESPACE_PATTERN</span><span class="symbol">);</span><span class="normal"> </span><span class="comment">// but let's do it anyway</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> result</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream and returns it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">String</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">String</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">readString</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">next</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream, parses it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">int</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and returns the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">int</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">int</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">readInt</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextInt</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream, parses it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">double</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and returns the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">double</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">double</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">readDouble</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextDouble</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream, parses it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">float</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and returns the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">float</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">float</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">float</span><span class="normal"> </span><span class="function">readFloat</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextFloat</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream, parses it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">long</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and returns the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">long</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">long</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">long</span><span class="normal"> </span><span class="function">readLong</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextLong</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream, parses it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">short</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and returns the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">short</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">short</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">short</span><span class="normal"> </span><span class="function">readShort</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextShort</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream, parses it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">byte</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and returns the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">byte</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="keyword">&lt;p&gt;</span>
<span class="comment">     * To read binary data, use {</span><span class="type">@link</span><span class="comment"> BinaryIn}.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">byte</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">byte</span><span class="normal"> </span><span class="function">readByte</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> scanner</span><span class="symbol">.</span><span class="function">nextByte</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads the next token from this input stream, parses it as a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">boolean</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * (interpreting either </span><span class="keyword">&lt;tt&gt;</span><span class="comment">"true"</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">"1"</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> as </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and either </span><span class="keyword">&lt;tt&gt;</span><span class="comment">"false"</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">"0"</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> as </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next </span><span class="keyword">&lt;tt&gt;</span><span class="comment">boolean</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in this input stream</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">readBoolean</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> </span><span class="function">readString</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equalsIgnoreCase</span><span class="symbol">(</span><span class="string">"true"</span><span class="symbol">))</span><span class="normal">  </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equalsIgnoreCase</span><span class="symbol">(</span><span class="string">"false"</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"1"</span><span class="symbol">))</span><span class="normal">               </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"0"</span><span class="symbol">))</span><span class="normal">               </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">InputMismatchException</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens from this input stream and returns them as</span>
<span class="comment">     * an array of strings.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining tokens in this input stream, as an array of strings</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> String</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllStrings</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="comment">// we could use readAll.trim().split(), but that's not consistent</span>
<span class="normal">        </span><span class="comment">// since trim() uses characters 0x00..0x20 as whitespace</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> tokens </span><span class="symbol">=</span><span class="normal"> WHITESPACE_PATTERN</span><span class="symbol">.</span><span class="function">split</span><span class="symbol">(</span><span class="function">readAll</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">tokens</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> tokens</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> tokens</span><span class="symbol">;</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> decapitokens </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> String</span><span class="symbol">[</span><span class="normal">tokens</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> tokens</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            decapitokens</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> tokens</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> decapitokens</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining lines from this input stream and returns them as</span>
<span class="comment">     * an array of strings.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining lines in this input stream, as an array of strings</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> String</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllLines</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">ArrayList&lt;String&gt;</span><span class="normal"> lines </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> ArrayList</span><span class="symbol">&lt;</span><span class="normal">String</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasNextLine</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            lines</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="function">readLine</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> lines</span><span class="symbol">.</span><span class="function">toArray</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> String</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens from this input stream, parses them as integers,</span>
<span class="comment">     * and returns them as an array of integers.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining lines in this input stream, as an array of integers</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllInts</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> fields </span><span class="symbol">=</span><span class="normal"> </span><span class="function">readAllStrings</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vals </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vals</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">fields</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> vals</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all remaining tokens from this input stream, parses them as doubles,</span>
<span class="comment">     * and returns them as an array of doubles.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all remaining lines in this input stream, as an array of doubles</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readAllDoubles</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> fields </span><span class="symbol">=</span><span class="normal"> </span><span class="function">readAllStrings</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> vals </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[</span><span class="normal">fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> fields</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vals</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Double</span><span class="symbol">.</span><span class="function">parseDouble</span><span class="symbol">(</span><span class="normal">fields</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> vals</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">///// end: section (2 of 2) of code duplicated from In to StdIn */</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Closes this input stream.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">close</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        scanner</span><span class="symbol">.</span><span class="function">close</span><span class="symbol">();</span><span class="normal">  </span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all integers from a file and returns them as</span>
<span class="comment">     * an array of integers.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">      filename the name of the file</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment">     the integers in the file</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by </span><span class="keyword">&lt;tt&gt;</span><span class="comment">new In(filename)</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.{</span><span class="type">@link</span><span class="comment"> #readAllInts()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readInts</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">).</span><span class="function">readAllInts</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads all doubles from a file and returns them as</span>
<span class="comment">     * an array of doubles.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">      filename the name of the file</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment">     the doubles in the file</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by </span><span class="keyword">&lt;tt&gt;</span><span class="comment">new In(filename)</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.{</span><span class="type">@link</span><span class="comment"> #readAllDoubles()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readDoubles</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">).</span><span class="function">readAllDoubles</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads all strings from a file and returns them as</span>
<span class="comment">     * an array of strings.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">      filename the name of the file</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment">     the strings in the file</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by </span><span class="keyword">&lt;tt&gt;</span><span class="comment">new In(filename)</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.{</span><span class="type">@link</span><span class="comment"> #readAllStrings()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> String</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readStrings</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">).</span><span class="function">readAllStrings</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads all integers from standard input and returns them</span>
<span class="comment">     * an array of integers.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment">     the integers on standard input</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> StdIn#readAllInts()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readInts</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">().</span><span class="function">readAllInts</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads all doubles from standard input and returns them as</span>
<span class="comment">     * an array of doubles.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment">     the doubles on standard input</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> StdIn#readAllDoubles()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readDoubles</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">().</span><span class="function">readAllDoubles</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Reads all strings from standard input and returns them as</span>
<span class="comment">     *  an array of strings.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment">     the strings on standard input</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> StdIn#readAllStrings()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> String</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">readStrings</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">().</span><span class="function">readAllStrings</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">    </span>
<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">In</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">In</span><span class="normal"> in</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> urlName </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"http://introcs.cs.princeton.edu/stdlib/InTest.txt"</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// read from a URL</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readAll() from URL "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> urlName</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">urlName</span><span class="symbol">);</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">in</span><span class="symbol">.</span><span class="function">readAll</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// read one line at a time from URL</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readLine() from URL "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> urlName</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">urlName</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">in</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// read one string at a time from URL</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readString() from URL "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> urlName</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">urlName</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">in</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readString</span><span class="symbol">();</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>


<span class="normal">        </span><span class="comment">// read one line at a time from file in current directory</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readLine() from current directory"</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="string">"./InTest.txt"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">in</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>


<span class="normal">        </span><span class="comment">// read one line at a time from file using relative path</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readLine() from relative path"</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="string">"../stdlib/InTest.txt"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">in</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// read one char at a time</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readChar() from file"</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="string">"InTest.txt"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">in</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">char</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readChar</span><span class="symbol">();</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">c</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// read one line at a time from absolute OS X / Linux path</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readLine() from absolute OS X / Linux path"</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="string">"/n/fs/introcs/www/java/stdlib/InTest.txt"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">in</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>


<span class="normal">        </span><span class="comment">// read one line at a time from absolute Windows path</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"readLine() from absolute Windows path"</span><span class="symbol">);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------------------------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="string">"G:</span><span class="specialchar">\\</span><span class="string">www</span><span class="specialchar">\\</span><span class="string">introcs</span><span class="specialchar">\\</span><span class="string">stdlib</span><span class="specialchar">\\</span><span class="string">InTest.txt"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">in</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> in</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Sat Oct 24 19:31:17 EDT 2015.
</small></address>

</html>
